<template>
	<view class="content">
		<u-navbar :autoBack="true" title="发布内容" :placeholder="true"  bgColor="#fff" :fixed="true" @leftClick="goHome" >
			<view slot="right">
				<text style="padding: 5px 16px;border-radius: 12px;background: #45C4B0;font-size: 10px;color: #fff;" @click="submit">提交</text>
			</view>
		</u-navbar>
		<view class="baibox">
			<view class="display-flex just-between" style="border-bottom: 1px solid rgba(153, 153, 153, 0.1);">
				<input type="text" class="put" placeholder="请输入标题..." v-model="form.title" maxlength="20">
				<text style="color: #999;font-size: 20rpx;">{{ form.title ? 20 - form.title.length : 20}}</text>
			</view>
			<textarea class="area" maxlength="10000000000000" placeholder="请输入正文..." v-model="form.row"></textarea>

			<view class="imgView" v-if="type!=1">
				<!-- <view v-for="(img,index) in form.img" class="imgList">
					<image :src="img | formatImgUrl" mode="aspectFill" style="width: 190rpx;height: 190rpx;"></image>
					<view class="imgClose" @click.stop="deletePic(index)">×</view>
				</view>
				<image :src="'/images/up.png' | formatImgUrl" mode="aspectFill" style="width: 190rpx;height: 190rpx;"
					@click="onChooseImg" v-if="form.img.length < 9"></image> -->
					
				<u-upload ref="uUpload" :uploadIcon="'/images/up.png' | formatImgUrl" :multiple="true" maxCount="9" width="105" height="105" :action="uploadImgUrl" :fileList="form.img" @afterRead='afterRead' @delete="deletePic">
				</u-upload>
			</view>

			<!-- <view class="waibox" @click="getGroupList(),quanshow = true">
				<view class="zebox"> 
					<image :src="'/images/ze.png' | formatImgUrl" class="zepic" mode=""></image>
					<view class="zetxt">{{group_name?group_name:'选择圈子'}}</view>
				</view>
			</view> -->
			<!-- <view class="waibox" @click="show = true">
				<view v-if="form.tags.length" style="display: flex;flex-wrap: wrap;">
					<view class="zebox" v-for="item in form.tags" style="margin-right: 10rpx;">
						<image :src="'/images/ze.png' | formatImgUrl" class="zepic" mode=""></image>
						<view class="zetxt">
							#{{item}}
						</view>
					</view>
				</view>
				<view class="zebox" v-else>
					<image :src="'/images/ze.png' | formatImgUrl" class="zepic" mode=""></image>
					<view class="zetxt">
						<text>选择标签</text>
					</view>
				</view>
			</view> -->


		</view>
		<view class="baibox">
			<view class="choose" @click="getGroupList(),quanshow = true">
				<view style="display: flex;align-items: center;">
					<image :src="'/images/home_need.png' | formatImgUrl" mode="" class="icon"></image>
					<view class="c_text">

						{{group_name?group_name:'选择圈子'}}


						<text>(曝光·可以更多地展示给其他人)</text>
					</view>
				</view>
				<image class="img_b" :src="'/images/ss3.png'  | formatImgUrl" mode=""></image>
			</view>
		</view>

		<view class="baibox">
			<view class="choose" @click="show = true">
				<view style="display: flex;align-items: center;">
					<image :src="'/images/biaoqian.png' | formatImgUrl" mode="" class="icon"></image>
					<view class="c_text">
						选择标签
						<text>(推荐·可以精准推荐给想看的人)</text>
					</view>
				</view>
				<image class="img_b" :src="'/images/ss3.png'  | formatImgUrl" mode=""></image>
			</view>
			<scroll-view scroll-x="true" class="tagbox" v-if="form.tags.length">
				<view class="tags" v-for="item in form.tags">
					{{item}}
				</view>
			</scroll-view>
		</view>

		<!-- <view class="subbox" @click="submit">
			{{isEdit?'上传审核':'提交'}}
		</view> -->

		<u-checkbox-group placement="row" @change="checkboxChange">
			<u-checkbox activeColor="#999999" inactiveColor="#999999" iconColor="#999999" labelColor="#999999" size="16"
				shape="circle" v-for="(item, index) in radiolist" :label="item.name" :key="index" :name="item.name"
				:checked="item.checked"></u-checkbox>
			<!-- <u-checkbox activeColor="#999999" label="添加水印" shape="circle"></u-checkbox>
			<u-checkbox activeColor="#999999" label="设置隐私" shape="circle"></u-checkbox> -->
		</u-checkbox-group>

		<!-- 选择圈子 -->
		<u-popup :show="quanshow" @close="quanclose" @open="open">
			<view class="mask">
				<view class="manei">
					<view class="matitle">
						选择圈子
					</view>
					<view class="soubox">
						<image :src="'/images/search.png' | formatImgUrl" class="soupic" mode=""></image>
						<input type="text" class="souput" placeholder="搜索圈子" v-model="keyword" @input="getGroupList">
					</view>
					<view class="empytView" v-if="!groupList.total">
						<image :src="'/images/kong.png' | formatImgUrl" mode="widthFix" />
						<!-- <view>- 空列表 -</view> -->
					</view>
					<view class="huli" v-for="item in groupList.data">
						<image :src="item.img | formatImgUrl" class="head" mode=""></image>
						<view class="hucen">
							<view class="hutitle">
								{{item.name}}
							</view>
							<view class="hutxt">
								{{item.row_num}}帖子
								<text>{{item.fans_num}}粉丝</text>
							</view>
						</view>
						<view v-if="form.gid!=item.id" class="join"
							@click="quanshow = false,form.gid=item.id,group_name=item.name">
							选择
						</view>
						<view v-if="form.gid==item.id" class="join clone_qx"
							@click="quanshow = false,form.gid='',group_name=''">
							取消
						</view>

					</view>
				</view>
			</view>
		</u-popup>



		<!-- 选择标签 -->
		<u-popup :show="show" @close="close" @open="open">
			<view class="mask">
				<view class="manei">
					<view class="matitle">
						选择标签
					</view>
					<view class="zebox">
						<view class="zetitle">
							选择标签
						</view>
						<view class="zetxt">
							还可添加{{5 - form.tags.length}}个标签
						</view>
					</view>

					<scroll-view scroll-x="true" class="biaobox">
						<view class="biaoli" v-for="(item,index) in form.tags">
							<view class="btxt">
								#{{item}}
							</view>
							<image :src="'/images/xxxx.png' | formatImgUrl" class="baix" mode="" @click="delTag(index)">
							</image>
						</view>
					</scroll-view>

					<view class="zebox">
						<view class="zetitle">
							自定义标签
						</view>
						<view class="zengtxt" @click="addTag">
							增加
						</view>
					</view>
					<view class="rubox">
						<textarea maxlength="20" class="area" placeholder="输入自定义标签，最多20个字符" v-model="tag"></textarea>
						<view class="shutxt">
							{{tag.length}}/20
						</view>
					</view>
				</view>
				<!-- <view class="qubox">
					<view class="subbox" @click="show = false">
						确认
					</view>
				</view> -->
			</view>
		</u-popup>

		<!-- <tnWaterMater @pBackImage="backImage" ref="childWaterMater"></tnWaterMater>
		<image :src="item" v-for="item in form.sy_img" mode="aspectFit" style="width: 200rpx;height: 200rpx;"></image> -->

	</view>
</template>
<script>
	import tnWaterMater from '../../components/tn-waterMark/index.vue'
	import * as db from '@/common/db.js'
	export default {
		data() {
			return {
				form: {
					img: [],
					tags: [],
					sy_img: []
				},
				// 选择圈子
				quanshow: false,
				keyword: '',
				groupList: {},
				group_name: '',
				// 标签
				show: false,
				tags: [],
				tag: '',
				isEdit: false,
				isClick: false,
				type: 0,
				// radiolist:[{name:'设置隐私',checked:false},{name:'设置例文',checked:false}],
				radiolist: [],
				checked: [],
				lw: 0,
				is_hide: 0,
				is_yin: 0,
				pos_w: 0,
				pos_h: 0,
				userInfo: this.$db.get('userInfo'),
				fileList2: []
			}
		},
		components: {
			tnWaterMater
		},
		onPullDownRefresh() {
			setTimeout(function() {
				uni.stopPullDownRefresh(); //停止当前页面下拉刷新	
			}, 500);

		},
		async onLoad(e) {
			// 写手有设置例文权限
			if (this.$db.get('userInfo').is_writer == 1) {
				this.radiolist = [{
					name: '设置隐私',
					checked: false
				}, {
					name: '设置例文',
					checked: false
				}, {
					name: '添加水印',
					checked: false
				}]
			} else {
				this.radiolist = [{
					name: '设置隐私',
					checked: false
				}, {
					name: '添加水印',
					checked: false
				}]
			}

			if (e.item) {
				this.form = JSON.parse(decodeURIComponent(e.item))
				this.fileList2 = this.form.img
				let img = this.form.img
				let imgArr = []
				for(let i in img){
					imgArr.push({
						thumb: img[i]
					})
				}
				
				this.form.img = imgArr
				
				this.group_name = this.form.group && this.form.group.name
				this.isEdit = true
			}
			console.log('gid', e)
			if (e.gid && e.group_name) {
				this.form.gid = e.gid
				this.group_name = e.group_name
			}
			if (e.type) {
				this.type = e.type;
			}
		},
		methods: {
			backImage(sy_img) {
				console.log(sy_img, 'backImage');

				this.$common.uploadImg(sy_img, (data) => {
					if (data) {
						console.log(data);
						this.form.sy_img.push(data)
					}
				})
			},
			addWaterBase64(imgurl) {
				let _this = this;
				let model = {};
				model.url = imgurl;
				// model.maxSize = 100;
				model.quality = 1
				model.watermark = [{
						type: 'image',
						path: 'https://coscdn.dreamyuewen.com/shuiyin3.png',
						rotate: 0,
						textAlign: 'center',
						textBaseline: 'center',
						isRepeat: false
					},
					{
						type: 'text',
						fillStyle: '#000',
						content: _this.userInfo.username,
						rotate: 0,
						textAlign: 'center',
						textBaseline: 'center',
						isRepeat: false
					}
				];
				this.$refs.childWaterMater.addWaterMark(model);
				console.log(654321);
			},
			checkboxChange(n) {
				// this.lw = n.length

				var check_arr = n.filter(item => item == '设置例文')
				this.lw = check_arr.length

				var check_arr2 = n.filter(item => item == '设置隐私')
				this.is_yin = check_arr2.length

				var check_arr3 = n.filter(item => item == '添加水印')
				this.is_hide = check_arr3.length

				console.log('change', this.lw, this.is_yin, this.is_hide, n);
			},
			submit() {


				let auth = db.get('auth');
				console.log(auth, 777777777)
				//用户存在，不跳转，不存在直接跳转
				if (!auth) {
					// toLogin()
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				}

				// if (this.isClick) {
				// 	return; // 如果已经禁止点击，则直接返回
				// }
				// this.isClick = true
				// if (this.isClick) {
				this.form.lw = this.lw; //例文
				this.form.is_hide = this.is_yin; //隐私
				this.form.is_yin = this.is_hide; //水印
				
				
				this.form.img = this.fileList2
				
				this.$api.default.request('Discover/addArticle', this.form, 'POST').then((res) => {
					if (res.code) {
						this.$common.successToShow(res.msg, () => {
							uni.redirectTo({
								url: '/pages/find/content?id=' + res.data.id
							})
						})
					}
				})
				// }
			},
			getGroupList() {
				this.$api.default.request('Discover/hotList', {
					keyword: this.keyword,
					limit: 999
				}, 'POST').then((res) => {
					if (res.code) {
						this.groupList = res.data
					}
				})
			},
			addTag() {
				let auth = db.get('auth');
				console.log(auth, 777777777)
				//用户存在，不跳转，不存在直接跳转
				if (!auth) {
					// toLogin()
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				}

				if (this.tag == '') {
					this.$common.errorToShow('请填写标签内容')
					return
				}
				var is = true
				this.form.tags.forEach(item => {
					if (item === this.tag) {
						is = false
						this.$common.errorToShow('请勿重复添加')
						return
					}
				})

				if (this.form.tags.length >= 5) {
					is = false
					this.$common.errorToShow('最多添加5个标签')
					return
				}

				if (is) {
					this.form.tags.push(this.tag)
				}
				this.tag = ''
			},
			delTag(index) {
				let auth = db.get('auth');
				console.log(auth, 777777777)
				//用户存在，不跳转，不存在直接跳转
				if (!auth) {
					// toLogin()
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				}

				this.form.tags.splice(index, 1)
			},

			//选择圈子弹框
			quanclose() {
				let auth = db.get('auth');
				console.log(auth, 777777777)
				//用户存在，不跳转，不存在直接跳转
				if (!auth) {
					// toLogin()
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				}
				this.quanshow = false
			},
			//选择标签弹框
			open() {
				// console.log('open');
			},
			close() {
				this.show = false
			},

			// 删除图片
			deletePic(index) {
				this.form.img.splice(index, 1)
				this.fileList2.splice(index, 1)
			},
			async onChooseImg() {
				let auth = db.get('auth');
				console.log(auth, 777777777)
				//用户存在，不跳转，不存在直接跳转
				if (!auth) {
					// toLogin()
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return;
				}

				// await this.$common.chooseImage({}, (data) => {
				// 	if (data) {
				// 		this.form.img.push(data)
				// 		// this.addWaterBase64(data)
				// 	}
				// }, 9)
				
				

			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.form.img.length
				lists.map((item) => {
					this.form.img.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.form.img[fileListLen]
					this.form.img.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.uploadImgUrl,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							let datas = JSON.parse(res.data)
							this.fileList2.push(datas.url)
							setTimeout(() => {
								resolve(datas.url)
							}, 300)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	/deep/.u-checkbox__icon-wrap--circle {
		background-color: rgba(242, 245, 242, 1) !important;
		border-color: #999 !important;
	}

	/deep/.u-checkbox-group text {
		color: #999;
		font-size: 12px !important;
	}

	page {
		background: rgba(242, 245, 242, 1);
	}

	.content {
		padding-top: 18rpx;

		.u-checkbox-group {
			display: flex;
			flex-direction: row;
			justify-content: flex-end;
			margin-top: 20rpx;
			margin-right: 40rpx;

			.u-checkbox {
				margin-right: 28rpx;
				font-size: 24rpx;
				color: #999999;
			}
		}

		.baibox {
			background-color: #ffffff;
			padding: 30rpx;
			margin-bottom: 20rpx;

			.choose {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.icon {
					width: 32rpx;
					height: 32rpx;
					object-fit: cover;
					margin-right: 8rpx;
				}

				.c_text {
					font-size: 28rpx;
					font-weight: bold;
					color: #3D3D3D;
					margin-left: 8rpx;

					text {
						font-weight: normal;
						font-size: 24rpx;
						color: #999999;
					}
				}

				.img_b {
					width: 20px;
					height: 20px;
				}
			}

			.tagbox {
				width: 100%;
				white-space: nowrap;
				padding-top: 20rpx;
				border-top: 1px solid rgba(153, 153, 153, 0.1);

				.tags {
					display: inline-block;
					vertical-align: middle;
					font-size: 24rpx;
					font-weight: normal;
					color: #999999;
					margin-left: 16rpx;
					padding: 4rpx 8rpx;
					border-radius: 178rpx;
					background: rgba(153, 153, 153, 0.1);
				}
			}

			.imgView {
				display: flex;
				align-items: center;
				flex-wrap: wrap;

				.imgList:nth-child(3n) {
					margin-right: 0;
				}

				.imgList {
					position: relative;
					padding-bottom: 10rpx;
					margin-right: 31rpx;

					.imgClose {
						position: absolute; 
						top: -8px; 
						right: -8px;
						z-index: 1;
						width: 36rpx;
						height: 36rpx;
						background-color: #45C4B0;
						color: #fff;
						line-height: 36rpx;
						text-align: center;
						border-radius: 100px;
					}
				}

				.imgList:not(:first-child) {
					// margin-left: 15rpx;
				}
			}

			.put {
				width: 610rpx;
				font-size: 28rpx;
				color: #3d3d3d;
				padding: 0 8rpx 20rpx;
				// border-bottom: 1px solid rgba(153, 153, 153, 0.1);
			}

			.area {
				width: 610rpx;
				padding: 20rpx 8rpx;
				max-height: 500rpx;
				min-height: 200rpx;
				resize: none;
				font-size: 28rpx;
				color: #3d3d3d;
			}

			.waibox {
				display: flex;
			}

			.zebox {
				margin-top: 20rpx;
				height: 38rpx;
				border-radius: 18px;
				opacity: 1;
				background: rgba(153, 153, 153, 0.1);
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 15rpx;

				.zepic {
					width: 32rpx;
					height: 32rpx;
				}

				.zetxt {
					font-size: 24rpx;
					font-weight: normal;
					letter-spacing: 0.08em;
					color: #3D3D3D;
					padding-left: 8rpx;

					.tags:not(:first-child) {
						padding-left: 10rpx;
					}
				}
			}
		}

		.subbox {
			width: 630rpx;
			height: 86rpx;
			border-radius: 8px;
			opacity: 1;
			background: #45C4B0;
			text-align: center;
			margin: 0 auto;
			line-height: 86rpx;
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
		}

		.mask {
			background: #F2F5F2;
			border-top-left-radius: 8px;
			border-top-right-radius: 8px;
			overflow: hidden;

			.manei {
				background-color: #ffffff;
				padding: 0 20px 40rpx;

				.matitle {
					font-size: 28rpx;
					font-weight: bold;
					letter-spacing: 0em;
					color: #3D3D3D;
					text-align: center;
					padding: 20rpx 0 32rpx;
				}

				.soubox {
					height: 100rpx;
					border-radius: 4px;
					padding: 0 40rpx;
					display: flex;
					align-items: center;
					opacity: 1;
					background: #F6F7F9;

					.soupic {
						width: 32rpx;
						height: 32rpx;
						flex-shrink: 0;
					}

					.souput {
						flex-grow: 1;
						padding-left: 12rpx;
						font-size: 28rpx;
						font-weight: normal;
						letter-spacing: 0em;
						color: #999999;
					}
				}

				.huli {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 0 30rpx;
					border-bottom: 1px solid rgba(153, 153, 153, 0.1);
					height: 134rpx;
					background-color: #ffffff;

					.head {
						width: 100rpx;
						height: 100rpx;
						border-radius: 50%;
						flex-shrink: 0;
					}

					.quanhead {
						width: 100rpx;
						height: 100rpx;
						border-radius: 12px;
						flex-shrink: 0;
					}

					.hucen {
						padding: 0 12rpx;
						flex-grow: 1;

						.hutitle {
							font-size: 28rpx;
							font-weight: bold;
							letter-spacing: 0em;
							color: #3D3D3D;
							padding-bottom: 8rpx;
						}

						.hutxt {
							font-size: 20rpx;
							color: #999999;

							text {
								padding-left: 20rpx;
								font-size: 20rpx;
								color: #999999;
							}
						}
					}

					.join {
						width: 104rpx;
						height: 40rpx;
						border-radius: 4px;
						opacity: 1;
						background: #45C4B0;
						text-align: center;
						line-height: 40rpx;
						font-size: 24rpx;
						font-weight: bold;
						color: #FFFFFF;
						flex-shrink: 0;
					}

					.clone_qx {
						width: 104rpx;
						height: 40rpx;
						border-radius: 4px;
						border: 1px solid #45C4B0;
						color: #45C4B0;
						opacity: 1;
						background: #fff;
						text-align: center;
						line-height: 40rpx;
						font-size: 24rpx;
						font-weight: bold;
						flex-shrink: 0;
					}
				}

				.huli:last-child {
					border-bottom: none;
				}

				.zebox {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding-bottom: 20rpx;

					.zetitle {
						font-size: 28rpx;
						font-weight: bold;
						letter-spacing: 0em;
						color: #3D3D3D;
					}

					.zetxt {
						font-size: 20rpx;
						font-weight: normal;
						text-align: right;
						letter-spacing: 0em;
						color: #999999;
					}

					.zengtxt {
						font-size: 28rpx;
						font-weight: normal;
						letter-spacing: 0em;
						color: #1DB994;
					}
				}

				.biaobox {
					width: 100%;
					padding-bottom: 10rpx;
					white-space: nowrap;

					.biaoli {
						padding: 0 10rpx;
						height: 38rpx;
						border-radius: 180px;
						opacity: 1;
						background: #1DB994;
						margin: 0 20rpx 10rpx 0;
						// display: flex;
						// align-items: center;
						// justify-content: center;
						display: inline-block;
						vertical-align: middle;
						position: relative;

						.btxt {
							font-size: 24rpx;
							font-weight: bold;
							letter-spacing: 0.08em;
							color: #FFFFFF;
							padding-right: 5rpx;
						}

						.baix {
							position: absolute;
							right: -1px;
							top: -1px;
							width: 24rpx;
							height: 24rpx;
						}
					}
				}

				.rubox {
					height: 150rpx;
					border-radius: 4px;
					opacity: 1;
					background: #F6F7F9;

					.area {
						padding: 20rpx 40rpx;
						font-size: 28rpx;
						letter-spacing: 0em;
						color: #333333;
						max-height: 60rpx;
					}

					.shutxt {
						font-size: 28rpx;
						font-weight: normal;
						text-align: right;
						letter-spacing: 0em;
						padding-right: 40rpx;
						color: #999999;
					}
				}
			}

			.qubox {
				margin-top: 20rpx;
				height: 120rpx;
				opacity: 1;
				background: #FFFFFF;
				display: flex;
				align-items: center;
				justify-content: center;

				.subbox {
					width: 630rpx;
					height: 80rpx;
					border-radius: 8px;
					opacity: 1;
					background: #45C4B0;
					font-size: 32rpx;
					line-height: 80rpx;
					font-weight: bold;
					text-align: center;
					letter-spacing: 0em;
					color: #FFFFFF;
				}
			}
		}

	}
</style>